import native from '@/libs/native';
import { mapState, mapActions } from "vuex";
export default {
    data() {
        return {
            popVisible:false,
            itemData:{},
        }
    },
    methods: {
        ...mapActions(['toggleLoading','showMsg','showModal']),
        //查看物流
        logisticsInfo(data){
            this.$router.push({path:'logisticsInfo',query:{orderId:data.id}});
        },
        //去发货
        logistics(data){
            this.$router.push({path:'logistics',query:{orderId:data.id}});
        },
        //退款
        refund(data,type){
            this.showModal({
                content: "确定"+(type=="REFUSE"?"拒绝退款":"同意退款")+"吗？",
                btns: ["取消", "确定"],
                callbacks: [()=>{}, ()=>{
                    this.toggleLoading({flag:true});
                    this.$ajax.applyRefundSell({orderId:data.id,type:type}).then(res=>{
                        this.toggleLoading({flag:false});
                        if(res.code=='0000'){
                            this.showMsg({ content: (type=="REFUSE"?"拒绝退款":"同意退款")+'成功' });
                            this.reload();
                        }
                    })
                }]
            });
        },
        //申请售后
        afterSale(data){
            this.showModal({
                content: "<p>申请售后请联系平台处理专员热线</p><p>15397043756</p>",
                btns:["确定"]
            });
        },
        //申请退款
        applyRefund(data){
            this.showModal({
                content: "<p>确定申请退款？</p><p>经卖家同意后，将立即原路退还至您的账户</p>",
                btns: ["取消", "确定"],
                callbacks: [()=>{}, ()=>{
                    this.toggleLoading({flag:true});
                    this.$ajax.applyRefundBuy({orderId:data.id}).then(res=>{
                        this.toggleLoading({flag:false});
                        if(res.code=='0000'){
                            this.showMsg({ content: '申请退款成功' });
                            this.reload();
                        }
                    })
                }]
            });   
        },
        //取消订单
        cancleOrder(data){
            this.showModal({
                content: "确定取消订单吗？",
                btns: ["取消", "确定"],
                callbacks: [()=>{}, ()=>{
                    this.toggleLoading({flag:true});
                    this.$ajax.orderCancel({orderId:data.id,type:'BUYER',type:'BUYER',remark:'买家取消'}).then(res=>{
                        this.toggleLoading({flag:false});
                        if(res.code=='0000'){
                            this.showMsg({ content: '取消订单成功' });
                            this.reload();
                        }
                    })
                }]
            }); 
        },
        //去支付
        pay(data){
            if(!this.version100){
                native.goPay(data.id).then(res=>{
                    this.reload();
                });
            }else{
                this.itemData = data;
                this.popVisible = true;
                this.$emit('payModel',data);
            }
        },
        //确定收货
        receivedGoods(data){
            this.showModal({
                content: "确定收货订单吗？",
                btns: ["取消", "确定"],
                callbacks: [()=>{}, ()=>{
                    this.toggleLoading({flag:true});
                    this.$ajax.orderReceiver({orderId:data.id}).then(res=>{
                        this.toggleLoading({flag:false});
                        if(res.code=='0000'){
                            this.showMsg({ content: '收货成功' });
                            this.reload();
                        }
                    })
                }]
            });
        },
        //支付方式
        payType(type){
            this.popVisible = false;
            native.goPay(this.itemData.id,type).then(res=>{
                this.reload();
            });
        }
    },
    computed:{
        ...mapState({
            userInfo: state => state.user.userInfo,
        }),
        //版本限制原生方法
        version100(){
            if(this.userInfo&&this.userInfo.appVersion){
              let appVersion = parseInt(this.userInfo.appVersion);
              if(appVersion>100){
                return true;
              }else{
                return false;
              }
            }
            return true;
        },
    }
}